package com.crm.system.service.impl.opportunityManager;

import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.opportunityManager.OpportunityProduct;
import com.crm.model.entity.opportunityManager.Phrase;
import com.crm.model.entity.sys.UserAccount;
import com.crm.service.opportunityManager.OpportunityPhraseDbService;
import com.crm.service.opportunityManager.OpportunityProductDbService;
import com.crm.system.service.opportunityManager.OpportunityPhraseService;
import com.crm.system.service.opportunityManager.OpportunityProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * bj
 * 商机产品
 */
@Service
public class OpportunityProductServiceImpl implements OpportunityProductService {

    @Autowired
    private OpportunityProductDbService opportunityProductDbService;

    /**
     * 商机-新增商机-查询商机下的产品
     * @param pageIndex 页码
     * @param pageSize 分页大小
     * @param sortName 排序列名字
     * @param sortOrder 排序规则
     * @param opportunityId 商机id
     * @return
     */
    @Override
    public DhPageInfo<OpportunityProduct> opportunityProductList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, String opportunityId,Integer whetherRenew) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        List<OpportunityProduct> list = opportunityProductDbService.opportunityProductList(opportunityId,"create_tm desc",whetherRenew);
        PageInfo<OpportunityProduct> pageInfo = new PageInfo<OpportunityProduct>(list);
        return new DhPageInfo<OpportunityProduct>(pageInfo, list);

    }

    /**
     * 商机管理-新增商机产品
     * @param opportunityProduct 商机产品实体
     * @return
     */
    @Override
    public String addOpportunityProduct(OpportunityProduct opportunityProduct) {

        List<OpportunityProduct> list = opportunityProductDbService.opportunityCodeAndOppIdList(opportunityProduct.getCustomerProductId(),opportunityProduct.getOpportunityId());
        if(list.size() == 0){
            UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
            String opportunityProductId = CommonUtils.getGUID();
            opportunityProduct.setOpportunityProductId(opportunityProductId);
            opportunityProduct.setCreatePeopleId(userAccount.getAccountId());
            opportunityProduct.setCreateTm(new Date());
            opportunityProduct.setUpdatePeopleId(userAccount.getAccountId());
            opportunityProduct.setUpdateTm(new Date());
            opportunityProduct.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
            Boolean result = opportunityProductDbService.addOpportunityProduct(opportunityProduct);
            return "success";
        }else{
            return "EX0001";
        }
    }

    /**
     * 商机-商机详情-修改商机产品回显
     * @param opportunityProductId 商机产品id
     * @return
     */
    @Override
    public OpportunityProduct getOpportunityProductById(String opportunityProductId) {
        return opportunityProductDbService.getOpportunityProductById(opportunityProductId);
    }

    /**
     * 商机管理-修改商机产品
     * @param opportunityProduct 商机产品实体
     * @return
     */
    @Override
    public Boolean updateOpportunityProduct(OpportunityProduct opportunityProduct) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        opportunityProduct.setUpdatePeopleId(userAccount.getAccountId());
        opportunityProduct.setUpdateTm(new Date());
        Boolean result = opportunityProductDbService.updateOpportunityProduct(opportunityProduct);
        return result;
    }

    /**
     * 商机管理-删除商机产品
     * @param opportunityProduct 商机产品实体
     * @return
     */
    @Override
    public Boolean deleteOpportunityProduct(OpportunityProduct opportunityProduct) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        opportunityProduct.setUpdatePeopleId(userAccount.getAccountId());
        opportunityProduct.setUpdateTm(new Date());
        opportunityProduct.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
        return opportunityProductDbService.updateOpportunityProduct(opportunityProduct);
    }
}
